<!-- 处理svg图 -->
<template>
  <!-- 展示外部组件 -->
  <div
    v-if="isExternal"
    :style="styleExternalIcon"
    class="svg-external-icon svg-icon"
    :class="className"
  ></div>
  <!-- 展示内部组件 -->
  <svg
    class="svg-icon"
    :class="className"
    :color="color"
    aria-hidden="true"
    :font-size="size"
  >
    <!-- :xlink:href 对应的iconName -->
    <use :xlink:href="iconName"></use>
  </svg>
</template>

<script setup>
import { isExternal as external } from '@/utils/validete'
import { defineProps, computed } from 'vue'
const props = defineProps({
  // icon图标
  icon: {
    // 类型
    type: String,
    // 必传
    required: true
  },
  // 图标类名
  className: {
    type: String,
    // 不传默认为空
    defaylt: ''
  },
  size: {
    type: String
  },
  color: {
    type: String
  }
})
/**
 * 判断当前图标是否为外部图标
 */
const isExternal = computed(() => external(props.icon))
/**
 * 外部图标
 */
const styleExternalIcon = computed(() => ({
  mask: `url(${props.icon}) no-repeat 50% 50%`,
  '-webkit-mask': `url(${props.icon}) no-repeat 50% 50%`
}))
/*
 * 内部图标
 **/
const iconName = computed(() => `#icon-${props.icon}`)
</script>

<style scoped lang='scss'>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
